/** 购物车相关服务 */

/**
 * [获取购物车商品列表（根据卖家分组）]
 * @return {[type]}          [description]
 */
function groups(callback){
  wx.request({
    url: getApp().globalData.host + 'cart/groups',
    header: getApp().globalData.header,
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data != null) {
        for(let i = 0;i<res.data.length;i++){
          for(let j = 0;j<res.data[i].items.length;j++){
            if(typeof(res.data[i].items[j].imgUrl)!='undefined'&&res.data[i].items[j].imgUrl.indexOf('img/')<2){ //处理图片兼容性
              res.data[i].items[j].imgUrl = getApp().globalData.imgHost + res.data[i].items[j].imgUrl;
            }
          }          
        }
        callback(res.data);
      }
    }
  })
}

/**
 * [全选/取消全选商品-重新计算运费等费用]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function clickAll(check,callback){
  let url = 'cart/checkAll';
  if (!check) url = 'cart/uncheckAll';
  wx.request({
    url: getApp().globalData.host + url,
    header: getApp().globalData.header,
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data.length > 0) {
        for(let i = 0;i<res.data.length;i++){
          for (let j = 0; j < res.data[i].items.length; j++){
            if(typeof(res.data[i].items[j].imgUrl)!='undefined'&&res.data[i].items[j].imgUrl.indexOf('img/')<2){ //处理图片兼容性
              res.data[i].items[j].imgUrl = getApp().globalData.imgHost + res.data[i].items[j].imgUrl;
            }
          }          
        }
        callback(res.data);
      }
    }
  })
}


/**
 * [选中/取消选中卖家所有商品-重新计算运费等费用]
 * @param  {Function} check [是否选中]
 * @param  {Function} seller [卖家ID]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function clickSeller(check,seller,callback){
  let url = 'cart/checkSeller';
  if (!check) url = 'cart/uncheckSeller';
  wx.request({
    url: getApp().globalData.host + url,
    header: getApp().globalData.header,
    data:{seller:seller},
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data.length > 0) {
        for(let i = 0;i<res.data.length;i++){
          for (let j = 0; j < res.data[i].items.length; j++){
            if(typeof(res.data[i].items[j].imgUrl)!='undefined'&&res.data[i].items[j].imgUrl.indexOf('img/')<2){ //处理图片兼容性
              res.data[i].items[j].imgUrl = getApp().globalData.imgHost + res.data[i].items[j].imgUrl;
            }
          }          
        }
        callback(res.data);
      }
    }
  })
}

/**
 * [选中单个商品-重新计算运费等费用]
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function clickSingle(check,sysNo,callback){
  let url = 'cart/checkSingle';
  if (!check) url = 'cart/uncheckSingle';
  wx.request({
    url: getApp().globalData.host + url,
    header: getApp().globalData.header,
    data:{sysNo:sysNo},
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data.length > 0) {
        for(let i = 0;i<res.data.length;i++){
          for (let j = 0; j < res.data[i].items.length; j++){
            if(typeof(res.data[i].items[j].imgUrl)!='undefined'&&res.data[i].items[j].imgUrl.indexOf('img/')<2){ //处理图片兼容性
              res.data[i].items[j].imgUrl = getApp().globalData.imgHost + res.data[i].items[j].imgUrl;
            }
          }          
        }
        callback(res.data);
      }
    }
  })
}

/**
 * [设置数量-重新计算运费等费用] 返回当前商品信息
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function setQty(sysNo, qty,callback) {
  wx.request({
    url: getApp().globalData.host + 'cart/setQty',
    header: getApp().globalData.header,
    data: { sysNo: sysNo, qty: qty },
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data.length > 0) {
        for (let i = 0; i < res.data.length; i++) {
          for (let j = 0; j < res.data[i].items.length; j++) {
            if (typeof (res.data[i].items[j].imgUrl) != 'undefined' && res.data[i].items[j].imgUrl.indexOf('img/') < 2) { //处理图片兼容性
              res.data[i].items[j].imgUrl = getApp().globalData.imgHost + res.data[i].items[j].imgUrl;
            }
          }
        }
        callback(res.data);
      }
    }
  })
}

/**
 * [设置数量-重新计算运费等费用] 返回seller（商家组）商品
 * @param  {Function} callback [description]
 * @return {[type]}            [description]
 */
function deleteSingle(sysNo, callback) {
  wx.request({
    url: getApp().globalData.host + 'cart/delete',
    header: getApp().globalData.header,
    data: { sysNo: sysNo },
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data != null) {
        for (let i = 0; i < res.data.length; i++) {
          for (let j = 0; j < res.data[i].items.length; j++) {
            if (typeof (res.data[i].items[j].imgUrl) != 'undefined' && res.data[i].items[j].imgUrl.indexOf('img/') < 2) { //处理图片兼容性
              res.data[i].items[j].imgUrl = getApp().globalData.imgHost + res.data[i].items[j].imgUrl;
            }
          }
        }
        callback(res.data);
      }
    }
  })
}

//确认订单页面获取订单数据
function orderToConfirm(orderId,callback){
  wx.request({
    url: getApp().globalData.host + 'order/orderToConfirmV2',
    header: getApp().globalData.header,
    data: { orderId: orderId },
    method: 'POST',
    success: function (res) {
      if (typeof (res.data) != 'undefined' && res.data != null) {
        for (let i = 0; i < res.data.childOrders.length; i++) {
          for (let j = 0; j < res.data.childOrders[i].skuMaps.length; j++) {
            if (typeof (res.data.childOrders[i].skuMaps[j].imgUrl) != 'undefined' && res.data.childOrders[i].skuMaps[j].imgUrl.indexOf('img/') < 2) { //处理图片兼容性
              res.data.childOrders[i].skuMaps[j].imgUrl = getApp().globalData.imgHost + res.data.childOrders[i].skuMaps[j].imgUrl;
            }
          }
        }
        callback(res.data);
      }
    }
  })
}

module.exports = {
  groups: groups,
  clickAll: clickAll,
  clickSeller: clickSeller,
  clickSingle: clickSingle,
  setQty: setQty,
  deleteSingle: deleteSingle,
  orderToConfirm:orderToConfirm
}